package com.hotchpotch.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by FuYu on 2017/1/17.
 */

public class SharedPreDataBase {
    private static SharedPreDataBase sharedPreDataBase;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    private SharedPreDataBase(Context context) {
        preferences = context.getSharedPreferences("hbShop", Context.MODE_PRIVATE);
        editor = preferences.edit();
    }

    public static SharedPreDataBase getPreferences(Context context) {
        if (sharedPreDataBase == null) {
            sharedPreDataBase = new SharedPreDataBase(context);
        }
        return sharedPreDataBase;
    }

    /**
     * 存储String字符串
     * @param name
     * @param content
     */
    public void saveStringData(String name, String content) {
        editor.putString(name, content);
        editor.apply();//异步执行操作
        editor.commit();
    }

    /**
     * 取出String字符串
     * @param name
     * @return
     */
    public String getStringData(String name) {
        String content = preferences.getString(name, "");
        return content;
    }

    public void saveIntData(String name, int content) {
        editor.putInt(name, content);
        editor.apply();
        editor.commit();
    }

    public void saveBooleanData(String name, boolean content) {
        editor.putBoolean(name, content);
        editor.apply();
        editor.commit();
    }

    public void saveFloatData(String name, float content) {
        editor.putFloat(name, content);
        editor.apply();
        editor.commit();
    }

    /**
     * 清除指定数据
     * @param name
     */
    public void removeData(String name) {
        editor.remove(name);
        editor.commit();
    }

    /**
     * 清空数据
     */
    public void clearData() {
        editor.clear();
        editor.commit();
    }
}
